package br.com.werp.regras.acesso.controle;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

import br.com.werp.regras.acesso.integracao.AcessoDAO;
import br.com.werp.regras.acesso.integracao.FabricaDAO;
import br.com.werp.regras.geral.Debuger;
import br.com.werp.regras.geral.vos.tabelas.Usuario;


/**
 * @author Sobreira
 * Created on 03/10/2005
 */
public class ControleAcesso {
    
    public boolean verificaUsuarioLogado(Usuario usuario)
        throws Exception {
        AcessoDAO acessoDAO = FabricaDAO.getAcessoDAO();
        Timestamp agora = new Timestamp(System.currentTimeMillis());
        Timestamp cincoMinAtraz = new Timestamp(System.currentTimeMillis());
        cincoMinAtraz.setTime(agora.getTime() - 300000);

        return acessoDAO.verificaUsuariosLogado(usuario, agora, cincoMinAtraz);
    }

    public static void main(String[] args) {
        Timestamp agora = new Timestamp(System.currentTimeMillis());
        Timestamp cincoMinAtraz = new Timestamp(System.currentTimeMillis());
        cincoMinAtraz.setTime(agora.getTime() - 300000);
        Debuger.debugar(agora, ControleAcesso.class);
        Debuger.debugar(cincoMinAtraz, ControleAcesso.class);
    }

    public boolean verificaUsuarioAcessoUrl(String usuario, String requestURI)
        throws Exception {
        AcessoDAO acessoDAO = FabricaDAO.getAcessoDAO();
        List urls = acessoDAO.obterUrlsUsuario(usuario);

        for (Iterator iter = urls.iterator(); iter.hasNext();) {
            String url = (String) iter.next();

            if (requestURI.startsWith(url)) {
                return true;
            }
        }

        return false;
    }
}
